package com.tengju.user.domain.model.star;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.tengju.user.domain.model.user.StarLevelEnum;
import com.tengju.user.domain.shared.AmountUtil;
import lombok.AllArgsConstructor;

import java.math.BigDecimal;
import java.util.List;

@AllArgsConstructor
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum StarIndicatorType {

    MANAGE_DEALER_NUM("Manage_Dealer_Num", "管理过的经销商人数","需区域经销商%s个,差%s,即可晋升为%s","需区域经销商","还差%s人"),
    CUSTOMER_MEMBERS_NUM("Customer_Members_Num","直属客户数(不含注册用户)","需直属客户%s个,差%s,即可晋升为%s","需直属客户","还差%s人"),
    DEVELOP_DEALER_NUM("Develop_Dealer_Num","培养过的经销商人数","需直属经销商%s个,差%s,即可晋升为%s","需直属经销商","还差%s人"),
    DEVELOP_CITY_SERVICE_NUM("Develop_City_Sevicer_Num","培养过城市服务商人数","需培育服务商%s个,差%s,即可晋升为%s","需培育服务商","还差%s人"),
    DEVELOP_SEC_NUM("Develop_Sec_Num","培养过sec人数","需培养SEC%s个,差%s，即可晋升为%s","需培养SEC","还差%s人"),
    RECENT_AVG_PROFIT("Recent_Avg_Profit","最近两个月服务费的平均值(1号上月和上上月平均,2号本月与上月平均)","近2个月平均服务费需%s,差%s,即可晋升为%s","近2个月平均服务费需","还差%s"),
    RECENT_TWO_MONTHS_AVG_ACHIEVEMENT("Recent_Two_Months_Avg_Achievement","最近2月销售额平均值(1号上月,2号及以后本月)","近2月平均业绩需%s,差%s,即可晋升为%s","近2个月平均业绩需","还差%s"),
    ;


    private String code;
    private String desc;
    private String indicatorDiff;
    private String upgradeCenterDesc;
    private String upgradeCenterDiff;

    public String getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }

    public String getIndicatorDiff() {
        return indicatorDiff;
    }

    public String getUpgradeCenterDesc() {
        return upgradeCenterDesc;
    }

    public String getUpgradeCenterDiff() {
        return upgradeCenterDiff;
    }

    public static List<StarIndicatorType> all() {
        return List.of(MANAGE_DEALER_NUM,
                CUSTOMER_MEMBERS_NUM,
                DEVELOP_DEALER_NUM,
                DEVELOP_CITY_SERVICE_NUM,
                DEVELOP_SEC_NUM,
                RECENT_AVG_PROFIT,
                RECENT_TWO_MONTHS_AVG_ACHIEVEMENT);
    }

    public String upgradeCenterDiff(BigDecimal indicatorDiffVal) {
        if (StarIndicatorType.RECENT_AVG_PROFIT == this
                || StarIndicatorType.RECENT_TWO_MONTHS_AVG_ACHIEVEMENT == this) {
            return String.format(this.getUpgradeCenterDiff(), AmountUtil.parseToString(indicatorDiffVal));
        }
        else if (StarIndicatorType.CUSTOMER_MEMBERS_NUM == this
                || StarIndicatorType.DEVELOP_DEALER_NUM == this
                || StarIndicatorType.DEVELOP_CITY_SERVICE_NUM == this
                || StarIndicatorType.DEVELOP_SEC_NUM == this
                || StarIndicatorType.MANAGE_DEALER_NUM == this) {
            return String.format(this.getUpgradeCenterDiff(), indicatorDiffVal.intValue());
        } else {
            return "";
        }
    }


    public static String indicatorDiff(StarIndicatorType type, BigDecimal indicatorDiffVal, BigDecimal targetIndicator, StarLevelEnum targetStarLevel) {
        if (StarIndicatorType.RECENT_AVG_PROFIT == type
                || StarIndicatorType.RECENT_TWO_MONTHS_AVG_ACHIEVEMENT == type) {
            return String.format(type.getIndicatorDiff(), AmountUtil.parseToString(targetIndicator),AmountUtil.parseToString(indicatorDiffVal),targetStarLevel.getShortName());
        }
        else if (StarIndicatorType.CUSTOMER_MEMBERS_NUM == type
                || StarIndicatorType.DEVELOP_DEALER_NUM == type
                || StarIndicatorType.DEVELOP_CITY_SERVICE_NUM == type
                || StarIndicatorType.DEVELOP_SEC_NUM == type
                || StarIndicatorType.MANAGE_DEALER_NUM == type) {
            return String.format(type.getIndicatorDiff(),targetIndicator.intValue(), indicatorDiffVal.intValue(),targetStarLevel.getShortName());
        } else {
            return "";
        }
    }

    public boolean isAmountIndex() {
        if (StarIndicatorType.RECENT_AVG_PROFIT == this
                || StarIndicatorType.RECENT_TWO_MONTHS_AVG_ACHIEVEMENT == this) {
            return true;
        }
        return false;
    }


    public static StarIndicatorType fromVal(String code) {
        for (StarIndicatorType type : values()) {
            if (type.code.equals(code)) {
                return type;
            }
        }
        return null;
    }




    public static StarIndicatorType fromName(String name) {
        for (StarIndicatorType type : values()) {
            if (type.name().equals(name)) {
                return type;
            }
        }
        return null;
    }



}
